class OrderMadeItem < ActiveRecord::Base
  
  validates :order_id,
    :presence =>true,
    :if => 'order_item_id.present?'  
  validates :installation_id,
    :presence =>true,
    :if => 'install_item_id.present?'
  validates :order_made_id,
    :presence => true
  validates :order_made_count,
    :numericality => {
      :greater_than_or_equal_to => 1
    }
    
  validates :furniture_id,
    :presence =>true
    
  belongs_to :order
  belongs_to :order_item
  belongs_to :installation
  belongs_to :install_item
  belongs_to :furniture
  belongs_to :order_made
  belongs_to :feed
  belongs_to :enter, :class_name => 'FurnitureLogisticStat',:foreign_key => 'enter_id'
  has_many :feeds, :dependent => :destroy  
  has_one :enter_item

  after_create do |order_made_item|            
    order_made_item.update_attribute :feed, Feed::OrderMadeItemCreated.create!(
      :order_id => order_made_item.order_id,
      :order_item_id => order_made_item.order_item_id,
      :installation_id => order_made_item.installation_id,
      :install_item_id => order_made_item.install_item_id,
      :furniture_id => order_made_item.furniture_id,
      :order_made_id => order_made_item.order_made_id,
      :order_made_item_id => order_made_item.id
    )
  end
  
  after_save do |order_made_item|
#    order_made_item.order_item.try(:'reset_feed!')
#    order_made_item.install_item.try(:'reset_feed!')

    order_made_item.order_item.try(:'reset_order_made_count!')
    order_made_item.install_item.try(:'reset_order_made_count!')
  end
  
  after_destroy do |order_made_item|
#    order_made_item.order_item.try(:'reset_feed!')
#    order_made_item.install_item.try(:'reset_feed!')
    
    order_made_item.order_item.try(:'reset_order_made_count!')
    order_made_item.install_item.try(:'reset_order_made_count!')
    order_made_item.order_item.try(:'order_made!')
    order_made_item.install_item.try(:'order_made!')
  end

  def status
    ActiveSupport::StringInquirer.new(feed.code)
  end

#  # 重置状态feed
#  def reset_feed!
#    update_attribute :feed_id, Feed.where(:order_made_item_id => id).last.id
#  end
  
  # 待订做的项, new_record? == true
  def self.unsents_by_factory_id(factory_id)
    (
      OrderItem::FactoryOrderMade.being_order_mades.where('furniture_styles.factory_id = ?',factory_id).includes(:furniture => :furniture_style).each_with_object([]) do |item,unsents|
        unsents << OrderMadeItem.new(
          :order_id => item.order_id,
          :order_item_id => item.id,
          :furniture_id => item.furniture_id,
          :order_made_count => item.can_be_order_made_count
        )
      end 
    ) + (
      InstallItem.being_order_mades.where('furniture_styles.factory_id = ?',factory_id).includes(:furniture => :furniture_style).each_with_object([]) do |item,unsents|
        unsents << OrderMadeItem.new(
          :order_id => item.order_id,
          :order_item_id => item.id,
          :installation_id => item.installation_id,
          :install_item_id => item.id,
          :furniture_id => item.furniture_id,
          :order_made_count => item.can_be_order_made_count
        )
      end
    )
  end
  
  # 来源
  def source
    ActiveSupport::StringInquirer.new(from)
  end
  
  def last_feed
    feeds.last
  end
  
  private
  def from
    case
    when installation.present?
      'after_serviced'
    when order.present?
      'ordered'
    else
      'example'
    end
  end
  
end
